package com.ykdl.member.kid.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

public class AssertTool {

	private static String tag = "AssertTool";
	public static final String DATA_PATH = "base_data";
	public static final String FOLDER_VERSION = "version";
	
	public static final String TYPE_CITY = "city";
	
	public static final String KEY_VERSION = "version";
	public static final String KEY_NAME= "name";
	
	public static final String BREAST_FEED = "breast_feed.txt";
	public static final String DEFECATE_COLOR = "defecate_color.txt";
	public static final String DEFECATE_SHAPE = "defecate_shape.txt";
	public static final String FEED_TYPE = "feed_type.txt";
	public static final String NAPPY = "nappy.txt";


	/**
	 * 列出base_data下得文件资源
	 * 
	 * @param context
	 * @return
	 */
	public static HashSet<String> listAssertFiles(Context context) {
		HashSet<String> hset = null;
		try {
			AssetManager assertManager = context.getAssets();
			String[] files = assertManager.list(DATA_PATH);
			if (null != files && files.length > 0) {
				hset = new HashSet<String>();
				for (String uni : files) {
					hset.add(uni);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
//			LogUtil.e(tag,
//					GlobalTool.getLogPrffix() + ":" + e.getLocalizedMessage());
		}
		return hset;
	}

	/**
	 * 打开assert的流
	 * 
	 * @param context
	 * @param srcPath
	 * @return
	 */
	public static InputStream getInputStreamFromAssertFile(Context context,
			String srcPath) {
		AssetManager assertManager = context.getAssets();
		InputStream is = null;
		try {
			is = assertManager.open(srcPath);
		} catch (Exception e) {
			e.printStackTrace();
//			LogUtil.e(tag,
//					GlobalTool.getLogPrffix() + ":" + e.getLocalizedMessage());
		}
		return is;
	}

	/**
	 * 使用GBK方式打开数据流
	 * 
	 * @param context
	 * @param srcPath
	 * @return
	 */
	public static ArrayList<String> readLinesFromAssertsFilesGBK(
			Context context, String srcPath) {

		InputStream is = getInputStreamFromAssertFile(context, srcPath);
		String line = null;
		ArrayList<String> list = null;
		BufferedReader bfr = null;
		if (null != is) {
			try {
				bfr = new BufferedReader(new InputStreamReader(is, "GBK"));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			list = new ArrayList<String>();
			try {
				while ((line = bfr.readLine()) != null) {
					list.add(line.trim());
				}
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
//				LogUtil.e(
//						tag,
//						GlobalTool.getLogPrffix() + " message:"
//								+ e.getLocalizedMessage());
			}
		}
		return list;

	}

	/**
	 * 按行读取文件
	 * 
	 * @param path
	 * @return
	 */
	public static ArrayList<String> readLinesFromAssertsFiles(Context context,
			String srcPath) {
		InputStream is = getInputStreamFromAssertFile(context, srcPath);
		if (null == is) {
			Log.e("wzy", srcPath + " not found..");
		} else {

		}
		String line = null;
		ArrayList<String> list = null;

		if (null != is) {
			BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
			list = new ArrayList<String>();
			try {
				while ((line = bfr.readLine()) != null) {
					list.add(line.trim());
				}
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
//				LogUtil.e(
//						tag,
//						GlobalTool.getLogPrffix() + " message:"
//								+ e.getLocalizedMessage());
			}
		}
		return list;
	}

	/**
	 * 从asserts目录中读取数据字典文件。json格式。
	 * 
	 * @param context
	 * @param filePath
	 * @return
	 */
	public static String readDataFromAssertsFile(Context context,
			String filePath) {
		InputStream is = getInputStreamFromAssertFile(context, filePath);
		if (null == is) {
//			LogUtil.e(tag, filePath + " not found..");
			return null;
		} else {

		}
		StringBuffer sb = new StringBuffer();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String str = null;
		try {
			while ((str = br.readLine()) != null) {
				sb.append(str);
			}
		} catch (IOException e) {

		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}
}
